package cate.game.activity.customwarorder;

import cate.common.table.activity.customwarorder.row.CustomWarOrderPayItemRow;
import cate.common.table.activity.customwarorder.row.CustomWarorderBaseRow;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.pay.PayItemRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.customwarorder.msg.CustomWarOrderResp;
import cate.game.chat.ChatContentParam;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass("玩家单个自选战令活动数据")
public class RoleCustomWarOrder extends RoleActivitySingle {
	@NoteField("锁定数据")
	public RoleCustomWarOrderChoose choose;

	@NoteField("等级数据")
	public RoleCustomWarOrderLevel level;

	@NoteField("任务数据")
	public RoleCustomWarOrderTask task;

	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (level == null) {
			level = new RoleCustomWarOrderLevel();
		}
		level.initialize(this);
		if (task == null) {
			task = new RoleCustomWarOrderTask();
		}
		task.initialize(this);
		if (choose == null) {
			choose = new RoleCustomWarOrderChoose();
		}
		choose.initialize(this);
	}

	@Override
	public void excelCheck() {
		level.excelCheck();
		task.excelCheck();
		choose.excelCheck();
	}


	public void beforeOver() {
		level.beforeOver(choose.chooseItems);
		task.beforeOver();
		choose.beforeOver();
	}

	public void clear() {
		level.clear();
		task.clear();
		choose.clear();
	}


	@Override
	public void onDaySpan(boolean silence) {
		super.onDaySpan(silence);
		level.onDaySpan(silence);
		task.onDaySpan(silence);
		choose.onDaySpan(silence);
	}

	@Override
	public void onWeekSpan(boolean silence) {
		task.onWeekSpan(silence);
	}

	@Override
	public void onRoleOpen() {

	}

	@Override
	public void onRoleEnd() {
		this.beforeOver();
		this.clear();
	}

	@Override
	public void initSend() {
		noticeUpdate();
	}

	@JsonIgnore
	public CustomWarorderBaseRow getRow() {
		return getRole().getGame().table.customWarorder.base.get(base.configTid);
	}

	public void noticeUpdate() {
		role.sendNow(new CustomWarOrderResp(this));
	}

	/**
	 * 激活检测
	 */
	@Override
	public GameResult<Void> allowPay(PayItemRow payItemRow) {
		GameResult<Void> r = new GameResult<>();
		if (payItemRow.funcSubId != this.configTid()) {
			return r.fail("活动已过期");
		}
		byte payItemType = getRow().getPayItemType(payItemRow.id);
		if (!level.allowPay(payItemType)) {
			return r.fail("支付类型有误");
		}
		return r.success();
	}


	@Override
	public void onPaySuccess(PayItemRow payItemRow) {
		CustomWarOrderPayItemRow warorderPayItemRow = role.getGame().table.customWarorder.pay.get(payItemRow.id);
		if (warorderPayItemRow == null) {
			return;
		}
		byte payItemType = getRow().getPayItemType(payItemRow.id);
		level.onPaySuccess(payItemType);

		CustomWarorderBaseRow row = getRow();
		level.addExp(warorderPayItemRow.exp);
		MixRes rwd = new MixRes(warorderPayItemRow.rwdStr);
		new MixResAdder().setRes(rwd).setOperation(GDOperation.A_WAR_ORDER).exe(role);
		rwd.addItem(new MixResItem(GDObj.Type.NUM, row.expItemId, warorderPayItemRow.exp));
		role.getGame().notice.dialogReward(getRole(), rwd);
		role.getGame().notice.message(getRole(), "激活成功");

		noticeUpdate();

		//跑马灯
		int noticeId = getRow().noticeId;
		if (noticeId > 0) {
			List<ChatContentParam> params = new ArrayList<>();
			params.add(ChatContentParam.c(role.getBase().name));
			params.add(ChatContentParam.c(ChatContentParam.TYPE_LANG_ID, row.nameId));
			role.getGame().notice.lampOnline(role, noticeId, params);
		}
	}

	@Override
	public void onMergeSettle(){
		level.onMergeSettle(choose.chooseItems);
	}

	public boolean hasChoosed() {
		return choose.hasChoosed();
	}
}
